import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/lf_badge_entity.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/pages/lf_tab_message/lf_tab_message_view.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/widgets/preload_page_view.dart';

import '../../entity/entities/lf_bool_entity.dart';
import '../../entity/entities/lf_user_entity.dart';
import '../../utils/lf_dialog_utils.dart';
import '../lf_me/lf_me_view.dart';
import '../lf_tab_contact/lf_tab_contact_view.dart';
import '../lf_tab_search/lf_tab_search_view.dart';
import '../lf_tab_spark/lf_tab_spark_view.dart';

class LfHomeLogic extends GetxController {
  List<Widget> pages = List.empty(growable: true);
  final pageController = PreloadPageController();
  RxInt selectedIndex = 0.obs;
  LfAuthService authService = Get.find();
  Rx<LfBadgeEntity> badgeEntity = LfBadgeEntity().obs;
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  @override
  void onInit() {
    pages.clear();
    pages.add(const LfTabSparkPage());
    pages.add(const LfTabSearchPage());
    pages.add(const LfTabMessagePage());
    pages.add(const LfTabContactPage());
    pages.add(const LfMePage());
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    refreshInfo();
    getBadge();
  }

  refreshInfo() {
    LfDio.instance.get<LfUserEntity>(url: apiProfile, params: {
      'profId': authService.currentAccount!.userId,
    }).then((value) {
      authService.setUserEntity(value);
      // showGuideAction(); TODO
      if (authService.currentAccount!.isGold() == false &&
          authService.currentAccount!.isHiddenItemOpen()) {
        ///如果非gold,悄么么的把hidden 给取消掉
        LfDio.instance.post<LfBoolEntity>(
            url: apiSetPrivacy,
            params: {'type': 'hidden_all', 'status': 0}).then((value) {
          hideLoading();
          authService.currentAccount!.hidden = 0;
          authService.currentAccount!.hiddenGender = 0;
          authService.setUserEntity(authService.currentAccount);
        }, onError: (error) {});
      }
    });
  }

  getBadge() {
    LfDio.instance
        .get<LfBadgeEntity>(
      url: apiNewBadge,
    )
        .then((value) {
      badgeEntity.value = value;
      badgeEntity.refresh();
    });
  }

  void changeValueBadges({required BadgeChangeType type, int? value}) {
    if (value != null) {
      switch (type) {
        case BadgeChangeType.message:
          badgeEntity.value.newMessage = value;
          break;
        case BadgeChangeType.visitedMe:
          badgeEntity.value.newVisitedMe = value;
          break;
        case BadgeChangeType.likedMe:
          badgeEntity.value.newLikedMe = value;
          break;
        default:
          break;
      }
    }
    badgeEntity.refresh();
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }
}

enum BadgeChangeType {
  visitedMe('visitedme'),
  likedMe('likedme'),
  message('type_new_message');

  final String value;
  const BadgeChangeType(this.value);

  @override
  String toString() => 'The change $name type is $value';
}
